package utils;

import org.apache.commons.io.IOUtils;
import play.Logger;
import play.Play;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;

public class ImgUtils {

    public static boolean thumbnail(File in, File out) {
        Process exec = null;
        try {
            String cmd = String.format(Play.configuration.getProperty("thumbnail.command", "convert %s %s"), in.getAbsolutePath(), out.getAbsolutePath());
            Logger.info(cmd);
            exec = Runtime.getRuntime().exec(cmd);
            exec.waitFor();
            StringWriter sw = new StringWriter(1024);
            IOUtils.copy(exec.getInputStream(), sw, "GBK");
            IOUtils.copy(exec.getErrorStream(), sw, "GBK");
            Logger.info(sw.toString());
            exec.destroyForcibly();
            return true;
        } catch (IOException | InterruptedException e) {
            Logger.error(e, e.getMessage());
            if (exec != null)
                try {
                    StringWriter sw = new StringWriter(1024);
                    IOUtils.copy(exec.getInputStream(), sw);
                    Logger.info(sw.toString());
                } catch (IOException e1) {
                }
            return false;
        }
    }

}